package com.gdssoft.pdaweb.action.am;
/*
 *  捷达世软件（深圳）有限公司 
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
*/
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.util.BeanUtil;
import com.gdssoft.core.web.action.BaseAction;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;
import com.gdssoft.pdaweb.service.am.PdaAmMeterService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import flexjson.JSONSerializer;
/**
 * 
 * @author 
 *
 */
public class PdaAmMeterAction extends BaseAction{
	@Resource
	private PdaAmMeterService pdaAmMeterService;
	
	private PdaAmMeter am;
	
	private Long meterId;

	public Long getMeterId() {
		return meterId;
	}

	public void setMeterId(Long meterId) {
		this.meterId = meterId;
	}

	public PdaAmMeter getPdaAmMeter() {
		return am;
	}

	public void setPdaAmMeter(PdaAmMeter pdaAmMeter) {
		this.am = pdaAmMeter;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<PdaAmMeter> list= pdaAmMeterService.getAll(filter);
		
		JSONSerializer serializer=new JSONSerializer();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");		
        buff.append(serializer.exclude(new String[] { "class" }).serialize(list));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	
//	/**
//	 * 批量删除
//	 * @return
//	 */
//	public String multiDel(){
//		
//		String[]ids=getRequest().getParameterValues("ids");
//		if(ids!=null){
//			for(String id:ids){
//				pdaAmMeterService.remove(new Long(id));
//			}
//		}
//		
//		jsonString="{success:true}";
//		
//		return SUCCESS;
//	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		PdaAmMeter pdaAmMeter=pdaAmMeterService.get(meterId);
		
		Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(pdaAmMeter));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		if(am.getAmId()==null){
			try {
				pdaAmMeterService.save(am);
			} catch (Exception e) {
				logger.error("SAVE pdaAmMeterList ERROR:"+e.getMessage());
				e.printStackTrace();
			}
		}else{
			PdaAmMeter orgPdaAmMeter=pdaAmMeterService.getByAmId(am.getAmId());
			try{
				BeanUtil.copyNotNullProperties(orgPdaAmMeter, am);
				am = pdaAmMeterService.save(orgPdaAmMeter);
			}catch(Exception ex){
				logger.error(ex.getMessage());
			}
		}
		setJsonString("{success:true}");
		return SUCCESS;
		
	}

}
